package com.hdu.clj.fitness

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_mod_pass.*

class modPassActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mod_pass)
        ActivityCollector.addActivity(this)

        passOK.setOnClickListener {
            if (accModLog.text.isNotEmpty()&&passOri.text.isNotEmpty()&&PassMod.text.isNotEmpty()){
                val acc = accModLog.text.toString()
                val pass = passOri.text.toString()
                val new_pass = PassMod.text.toString()
                val cur_acc = LoadAndSave().load("userAcc",this)
                if (acc.equals(cur_acc)){
                    val dbHelper = userDatabase(this,"Users.db",1)
                    val db = dbHelper.writableDatabase
                    var curUserData = db.rawQuery("select * from Users where acc = ?", arrayOf(acc))
                    var cur_pass = ""
                    if (curUserData.moveToFirst()){
                        cur_pass = curUserData.getString(curUserData.getColumnIndex("pass"))
                    }
                    if (pass.equals(cur_pass)){
                        db.execSQL("update Users set pass = ? where acc = ?", arrayOf(new_pass,acc))
                        Toast.makeText(this,"修改成功！",Toast.LENGTH_SHORT).show()
                    }
                    else{
                        Toast.makeText(this,"密码错误！",Toast.LENGTH_SHORT).show()
                    }

                }
                else{
                    Toast.makeText(this,"账号错误！",Toast.LENGTH_SHORT).show()
                }

            }
            else{
                Toast.makeText(this,"请输入完整信息",Toast.LENGTH_SHORT).show()
            }
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
    }
}
